{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# alldifferent_except_0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/alldifferent_except_0.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/alldifferent_except_0.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  All different except 0 Google CP Solver.\n",
    "\n",
    "  Decomposition of global constraint alldifferent_except_0.\n",
    "\n",
    "  From Global constraint catalogue:\n",
    "  http://www.emn.fr/x-info/sdemasse/gccat/Calldifferent_except_0.html\n",
    "  '''\n",
    "  Enforce all variables of the collection VARIABLES to take distinct\n",
    "  values, except those variables that are assigned to 0.\n",
    "\n",
    "  Example\n",
    "     (<5, 0, 1, 9, 0, 3>)\n",
    "\n",
    "  The alldifferent_except_0 constraint holds since all the values\n",
    "  (that are different from 0) 5, 1, 9 and 3 are distinct.\n",
    "  '''\n",
    "\n",
    "  Compare with the following models:\n",
    "  * Comet: http://hakank.org/comet/alldifferent_except_0.co\n",
    "  * ECLiPSe: http://hakank.org/eclipse/alldifferent_except_0.ecl\n",
    "  * Tailor/Essence': http://hakank.org/tailor/alldifferent_except_0.eprime\n",
    "  * Gecode: http://hakank.org/gecode/alldifferent_except_0.cpp\n",
    "  * Gecode/R: http://hakank.org/gecode_r/all_different_except_0.rb\n",
    "  * MiniZinc: http://hakank.org/minizinc/alldifferent_except_0.mzn\n",
    "  * SICStus_ http://hakank.org/sicstus/alldifferent_except_0.pl\n",
    "  * Choco: http://hakank.org/choco/AllDifferentExcept0_test.java\n",
    "  * JaCoP: http://hakank.org/JaCoP/AllDifferentExcept0_test.java\n",
    "  * Zinc: http://hakank.org/minizinc/alldifferent_except_0.zinc\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "#\n",
    "# Decomposition of alldifferent_except_0\n",
    "# Thanks to Laurent Perron (Google) for\n",
    "# suggestions of improvements.\n",
    "#\n",
    "\n",
    "\n",
    "def alldifferent_except_0(solver, a):\n",
    "  n = len(a)\n",
    "  for i in range(n):\n",
    "    for j in range(i):\n",
    "      solver.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j]))\n",
    "\n",
    "\n",
    "# more compact version:\n",
    "\n",
    "\n",
    "def alldifferent_except_0_b(solver, a):\n",
    "  n = len(a)\n",
    "  [\n",
    "      solver.Add((a[i] != 0) * (a[j] != 0) <= (a[i] != a[j]))\n",
    "      for i in range(n)\n",
    "      for j in range(i)\n",
    "  ]\n",
    "\n",
    "\n",
    "def main(unused_argv):\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Alldifferent except 0\")\n",
    "\n",
    "  # data\n",
    "  n = 7\n",
    "\n",
    "  # declare variables\n",
    "  x = [solver.IntVar(0, n - 1, \"x%i\" % i) for i in range(n)]\n",
    "  # Number of zeros.\n",
    "  z = solver.Sum([x[i] == 0 for i in range(n)]).VarWithName(\"z\")\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  alldifferent_except_0(solver, x)\n",
    "\n",
    "  # we require 2 0's\n",
    "  solver.Add(z == 2)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add([x[i] for i in range(n)])\n",
    "  solution.Add(z)\n",
    "\n",
    "  collector = solver.AllSolutionCollector(solution)\n",
    "  solver.Solve(\n",
    "      solver.Phase([x[i] for i in range(n)], solver.CHOOSE_FIRST_UNBOUND,\n",
    "                   solver.ASSIGN_MIN_VALUE), [collector])\n",
    "\n",
    "  num_solutions = collector.SolutionCount()\n",
    "  for s in range(num_solutions):\n",
    "    print(\"x:\", [collector.Value(s, x[i]) for i in range(n)])\n",
    "    print(\"z:\", collector.Value(s, z))\n",
    "    print()\n",
    "\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "main(\"cp sample\")\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
